% Same as smle_pairwise but with four drugs


clear
clc
% person, period, drugid, choice, copay, prev


% ignore serial correlations
% focus on class 2

% sketch
% fminsearch @ (t) SMLE(t, data, random_draws)
% SMLE function: simulate using random draws scaled by parameters
% compute a total likelihood of seeing data as a function of parameters
% Compute a Hessian of the function at optimal parameters



% personid quarter drugid chosen cost_sharing incumbent control_extra treat_extra
% starts in June 1996 (1)
% can go to May 2006 (40)
A = importdata('general_entry_choice_data_modified.txt');

data = A(:,1:6);
id = A(:,1);
quarter = A(:,2);
drugid = A(:,3);
chosen = A(:,4);
cost_sharing = A(:,5);
incumbent_id = A(:,6);

% % filtering
filter = (1-isnan(cost_sharing)); % CHANGED
id(filter == 0, :) = [];
quarter(filter == 0, :) = [];
drugid(filter == 0, :) = [];
chosen(filter == 0, :) = [];
cost_sharing(filter == 0, :) = [];
incumbent_id(filter == 0, :) = [];
data(filter == 0, :) = [];


% extra parameters: number of periods, etc.
num_drugs = max(drugid);
num_periods = max(quarter);
num_people = max(id);

% setup work common to each loop
[~,~,id] = unique([id quarter], 'rows'); % unique person/quarter combos => creates choice id
outside = 1 - accumarray(id, chosen); % chose the outside option


% draw some normal random numbers
rng(17)
S = 100;
random_draws = normrnd(0,1,num_people*num_drugs, S);
% NEW: structure is (person id - 1) * 4 + drugid (S draws for each column)
% set generic Zocor equal to branded Zocor
for i=1:num_people
   random_draws(num_drugs*(i-1)+4,:) = random_draws(num_drugs*(i-1)+1,:);
end

% parameters delta for each drug x period, alpha, gamma, sigma2 (of idiosyncratic)
% start with values that roughly reflect previous estimates
%delta_init = [-1;-0.5;-2];
delta_init = [-1.0970;-0.0839;-1.7948;-1.0970];
%params_init = [delta_init; 0.04; 5; 1];
gamma_init = [6.29 0 0 6.29; 0 5.56 0 0; 0 0 7.22 0; 0 0 0 3];
params_init = [delta_init; 0.0284; gamma_init(:,1);gamma_init(:,2);gamma_init(:,3);gamma_init(:,4);-1.391];
params = params_init;

myopts = optimset('TolFun', 1e-10, 'MaxFunEvals', 30000, 'MaxIter', 20000, 'display','iter'); % off, iter, final, notify
% previously 10^-8

% test
SMLE_v2(params_init,data,random_draws,num_drugs,num_periods,id,outside,S);

[params_opt, mle] = fminsearch (@(t)...
    -SMLE_v2(t,data,random_draws,num_drugs,num_periods,id,outside,S), params_init, myopts);

save('smle_c2.mat');

delta = params_opt(1:4);

alpha = params_opt(5);
gamma = [params_opt(6:9) params_opt(10:13) params_opt(14:17) params_opt(18:21)];
sigma = params_opt(22);

alpha
gamma
sigma



%% Compute standard errors
fun = @(t) -SMLE_v2(t,data,random_draws, num_drugs, num_periods, id, outside, S);


% hessian of a function at the final point
%[H_small, H, H_big, G] = own_hessian_v2(fun, params_opt);
[H_small, H, H_big, G] = own_hessian_v3(fun, params_opt);

n=length(params_opt);
for i=1:n
    for j=i+1:n
        H(i,j) = H(j,i);
        H_small(i,j) = H_small(j,i);
        H_big(i,j) = H_big(j,i);
    end
end

%variance_mat = inv(H_big);
variance_mat = inv(H_small);
standard_errors = diag(variance_mat.^(.5));



se_gamma = [standard_errors(6:9) standard_errors(10:13) standard_errors(14:17) standard_errors(18:21)];

gamma
se_gamma
mle

save('smle_c2_correlated.mat');


